formatx 0.1.4

A macro for formatting non literal strings at runtime in Rust
Documentation

This library aims for formatting strings and numbers rather than an generic type. Syntax for the format string is derived from std::fmt.

Getting Started

Add this to your Cargo.toml file.

[dependencies]
formatx = "0.1"

Or add from command line.

$ cargo add formatx@0.1

See docs and examples to know how to use it.

Example

SOURCE: format! with non literal string

use formatx::formatx;

fn message(language: &str, name: &str, number: i32) -> String {
    let s = match language {
        "french" => "Bonjour {}, le nombre est {}",
        "spanish" => "Hola {}, el numero es {}",
        _ => "Hi {}, the number is {}",
    };
    formatx!(s, name, number).unwrap()
}

fn main() {
    println!("{}", message("french", "Léa", 1));
    println!("{}", message("spanish", "Sofia", 2));
    println!("{}", message("english", "Ashley", 3));
}

OUTPUT

Bonjour Léa, le nombre est 1
Hola Sofia, el numero es 2
Hi Ashley, the number is 3

Limitations

Examples given below will always panic.

  1. Only types which implements Display + Debug traits are supported. Other formatting-traits aren't supported.

  2. Local variable interpolation isn't supported.

let people = "Rustaceans";
formatx!("Hello {people}!").unwrap().text().unwrap();
  1. Intermingling the two types of positional specifiers isn't supported.
formatx!("{1} {} {0} {}", 1, 2).unwrap();
  1. Parameter setting through $ sign argument isn't supported.
formatx!("{:width$}!", "x", width = 5).unwrap();
  1. An asterisk .* can't be used to set precision.
formatx!("{:.*}", 5, 0.01).unwrap();

Alternatives

  1. strfmt
  2. runtime-fmt

License

Dual Licensed